<?php

use Illuminate\Database\Seeder;

class InitDataSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        if (!DB::table('users')->count()) {
            $salt = Str::random(4);
            $time = time();
            DB::table('users')->insert([
                'username' => 'admin',
                'name' => '超级管理员',
                'password' => md5(md5('111111') . $salt),
                'salt' => $salt,
                'role_tag' => \App\Models\Role::PLAT,
                'created_at' => $time,
                'updated_at' => $time
            ]);
            \App\Models\Role::query()->insert([
                [
                    'name' => '系统管理员',
                    'tag' => \App\Models\Role::PLAT,
                    'created_at' => $time,
                    'updated_at' => $time
                ],
                [
                    'name' => '业务员',
                    'tag' => \App\Models\Role::STAFF,
                    'created_at' => $time,
                    'updated_at' => $time
                ],
                [
                    'name' => '业务部负责人',
                    'tag' => \App\Models\Role::STAFF_MANAGE,
                    'created_at' => $time,
                    'updated_at' => $time
                ]
            ]);
            $sql = file_get_contents(base_path('database/seeds/init.sql'));
            $sql = str_replace('$time', $time, $sql);
            DB::getPdo()->exec($sql);
        }
    }
}
